//
//  RegisterViewController.m
//  DailyRecord
//
//  Created by Gin on 2022/3/27.
//

#import "RegisterViewController.h"
#import "UITextField+PlaceHolder.h"
#import "NetWorkService.h"
#import "RegisterParamModel.h"
#import "UIImage+Resize.h"

@interface RegisterViewController ()

@property (nonatomic, strong) UILabel *accountLb;

@property (nonatomic, strong) UITextField *accountTf;

@property (nonatomic, strong) UILabel *pswLb;

@property (nonatomic, strong) UITextField *pswTf;

@property (nonatomic, strong) UILabel *confirmPswLb;

@property (nonatomic, strong) UITextField *confirmPswTf;

@property (nonatomic, strong) UIButton *registerBtn;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getAndBindData];
    [self setCustomNavigationTitle:@"注册"];
}

- (void)initUI{
    [self.view addSubview:self.accountLb];
    [self.view addSubview:self.accountTf];
    [self.view addSubview:self.pswLb];
    [self.view addSubview:self.pswTf];
    [self.view addSubview:self.confirmPswLb];
    [self.view addSubview:self.confirmPswTf];
    [self.view addSubview:self.registerBtn];
}

- (void)setUpContraints{
    [self.accountLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_centerX).offset(-30);
        make.top.mas_equalTo(160 + kNavigationBarHeight);
    }];
    
    [self.accountTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.accountLb);
        make.height.equalTo(self.accountLb);
        make.left.equalTo(self.accountLb.mas_right).offset(kLeftSpace);
        make.width.mas_equalTo(200);
    }];
    
    [self.pswLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.accountLb);
        make.top.equalTo(self.accountLb.mas_bottom).offset(kBottomSpace);
    }];
    
    [self.pswTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.pswLb);
        make.left.width.height.equalTo(self.accountTf);
    }];
    
    [self.confirmPswLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.accountLb);
        make.top.equalTo(self.pswLb.mas_bottom).offset(kBottomSpace);
    }];
    
    [self.confirmPswTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.confirmPswLb);
        make.left.width.height.equalTo(self.accountTf);
    }];
    
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.confirmPswLb.mas_bottom).offset(40);
        make.centerX.equalTo(self.view);
        make.left.mas_equalTo(kLeftSpace);
        make.height.mas_equalTo(kLargeButtonHeight);
    }];
}

- (void)getAndBindData{
    self.accountLb.text = @"账号 : ";
    self.pswLb.text = @"密码 : ";
    self.confirmPswLb.text = @"确认密码: ";
    [self.registerBtn setTitle:@"注册" forState:UIControlStateNormal];
}

- (Boolean)verify{
    if (self.accountTf.text.length == 0) {
        [MBProgressHUD showMessage:@"请输入账号" toView:self.view];
        return NO;
    }else if (self.pswTf.text.length == 0){
        [MBProgressHUD showMessage:@"请输入密码" toView:self.view];
        return NO;
    }else if (self.pswTf.text.length < 6){
        [MBProgressHUD showMessage:@"密码长度少于6位，请重新输入" toView:self.view];
        return NO;
    }else if (self.confirmPswTf.text.length == 0){
        [MBProgressHUD showMessage:@"请确认密码" toView:self.view];
        return NO;
    }else if (![self.confirmPswTf.text isEqualToString:self.pswTf.text]){
        [MBProgressHUD showMessage:@"两次密码不相等，请重新确认" toView:self.view];
        return NO;
    }
    return YES;
}

- (void)toRegister{
    RegisterParamModel *paramM = [RegisterParamModel new];
    paramM.username = self.accountTf.text;
    paramM.passwd = self.pswTf.text;
    NSDictionary *param = [paramM yy_modelToJSONObject];
    [MBProgressHUD showMessage:kInRequestText toView:self.view];
    [NetWorkService servicePostWithPath:kUserRegister parameters:param success:^(id  _Nullable responseObj) {
        [MBProgressHUD hideHUDForView:self.view];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError * _Nullable error) {
        [MBProgressHUD showError:kRequestFailText toView:self.view];
    }];
}

#pragma mark -- setter&&getter

- (UILabel *)accountLb{
    if (!_accountLb) {
        _accountLb = [UILabel new];
        _accountLb.font = kInfoFont;
    }
    return _accountLb;
}

- (UITextField *)accountTf{
    if (!_accountTf) {
        _accountTf = [UITextField new];
        _accountTf.font = kInfoFont;
        _accountTf.placeholder = @"请输入账号";
        [_accountTf setPlaceholderFont:_accountTf.font];
    }
    return _accountTf;
}

- (UILabel *)pswLb{
    if (!_pswLb) {
        _pswLb = [UILabel new];
        _pswLb.font = kInfoFont;
    }
    return _pswLb;
}

- (UITextField *)pswTf{
    if (!_pswTf) {
        _pswTf = [UITextField new];
        _pswTf.font = kInfoFont;
        _pswTf.placeholder = @"请输入密码(不少于6位)";
        [_pswTf setPlaceholderFont:_pswTf.font];
        [_pswTf setSecureTextEntry:YES];
    }
    return _pswTf;
}

- (UILabel *)confirmPswLb{
    if (!_confirmPswLb) {
        _confirmPswLb = [UILabel new];
        _confirmPswLb.font = kInfoFont;
    }
    return _confirmPswLb;
}

- (UITextField *)confirmPswTf{
    if (!_confirmPswTf) {
        _confirmPswTf = [UITextField new];
        _confirmPswTf.font = kInfoFont;
        _confirmPswTf.placeholder = @"请确认密码";
        [_confirmPswTf setPlaceholderFont:_confirmPswTf.font];
        [_confirmPswTf setSecureTextEntry:YES];
    }
    return _confirmPswTf;
}

- (UIButton *)registerBtn{
    if (!_registerBtn) {
        _registerBtn = [UIButton new];
        [_registerBtn.titleLabel setFont:kTitleFont];
        [_registerBtn setBackgroundImage:[[UIImage imageNamed:@"btn_bg"] resizeWithCenter] forState:UIControlStateNormal];
        @weakify(self);
        [[_registerBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            Boolean isPassVerify = [self verify];
            if (isPassVerify) {
                [self toRegister];
            }
        }];
    }
    return _registerBtn;
}

@end
